package com.weiyi.mashangzhu.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.OrderDetailActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    @BindView(R.id.result)
    TextView result;
    @BindView(R.id.img)
    ImageView img;

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        ButterKnife.bind(this);

        api = WXAPIFactory.createWXAPI(this, "wx29a520b1867fe3a2");

        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e("wxxxxxxxx", "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (String.valueOf(resp.errCode).equals("-1")) {
                result.setText("未能成功调起微信支付");
                img.setImageResource(R.mipmap.shibai);


            } else if (String.valueOf(resp.errCode).equals("-2")) {
                result.setText("未支付");
                img.setImageResource(R.mipmap.shibai);

            } else if (String.valueOf(resp.errCode).equals("0")) {
                result.setText("支付成功");
                img.setImageResource(R.mipmap.chenggong);
                OrderDetailActivity.intence.finish();

            }

//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.setMessage(getString(R.string.pay_result_callback_msg, resp.errStr + ";code=" + String.valueOf(resp.errCode)));
//			builder.show();
        }
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}